﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using D2D2.SharedInterfaces.Networking;
using Lidgren.Network;

namespace D2D2.Networking
{
    public class ClientManager
    {
        private NetClient _client;

        public void Start()
        {
            var config = new NetPeerConfiguration("D2D2")
            {
                SimulatedMinimumLatency = 0.1f,
                //SimulatedLoss = 0.1f
            };


            config.EnableMessageType(NetIncomingMessageType.WarningMessage);
            config.EnableMessageType(NetIncomingMessageType.ErrorMessage);
            config.EnableMessageType(NetIncomingMessageType.Error);
            config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            /*
            config.EnableMessageType(NetIncomingMessageType.VerboseDebugMessage);
            config.EnableMessageType(NetIncomingMessageType.DebugMessage);
            */

            //config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);

            _client = new NetClient(config);
            _client.Start();
            _client.Connect(new IPEndPoint(NetUtility.Resolve("127.0.0.1"), Convert.ToInt32("14242")));
        }

        public void SendMessage(IGameMessage gameMessage)
        {
            NetOutgoingMessage message = _client.CreateMessage();
            message.Write((byte)gameMessage.MessageType);
            message.WriteTime(false);
            gameMessage.Encode(message);

            _client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
        }

        public NetClient NetClient { get { return _client; } }
    }
}
